var $ = require('../../utils/Ajax.js');
Page({
  data:{
    Name: '',
    Name_: '',
    Number: '',
    Phone: '',
    Code: '',
    // 图片地址
    PictureUrl: getApp().PictureUrl,
    // 银行卡图标是否显示
    BankBol: false,
    // 银行卡图标
    BankLogo: null,
    // 银行卡类型
    BankName: '请选择卡类型',
    // 银行卡类型ID
    BankType: null,
    BtnText:'获取验证码',
    BtnTime: 60,
    Disabled: false,
    CodeBg: true
  },
  onLoad: function(){
    wx.setNavigationBarTitle({
      title: "添加银行卡"
    }) 
  },
  onShow: function (){
    if (this.data.BankType != null && this.data.BankLogo != null && this.data.BankName != null){
      this.setData({
        BankBol: true
      })
    }
  },
  Name: function(e){
    this.setData({
      Name: e.detail.value,
    });
  },
  Name_: function (e) {
    this.setData({
      Name_: e.detail.value,
    });
  },
  Number: function(e){
    this.setData({
      Number: e.detail.value,
    });
  },

  // 选择银行
  SelectBank: function(){
    wx.navigateTo({
      url: '../SelectBank/SelectBank',
    })
  },

  // 手机号码
  Tel: function(e){
    this.setData({
      Phone: e.detail.value
    });
  },

  // 点击获取验证码
  CodeBtn: function(){
    var that = this;
    var Phone = that.data.Phone
    console.log(Phone)
    if (Phone.length == 11){
      $.Ajax("api/x_sendmsg", "POST", {
        mob: Phone
      }, function (res){
        if (res.s){
          setInterval(function () {
            var BtnTime = --that.data.BtnTime
            if (BtnTime >= 0) {
              that.setData({
                Disabled: true,
                BtnText: '(' + BtnTime + '秒)'
              })
            } else {
              that.setData({
                Disabled: false,
                BtnText: '重新获取'
              })
            }
          }, 1000)
        }else{
          wx.showToast({
            title: res.m,
            icon: 'succes',
            image: '../../images/err.png',
            duration: 3000,
            mask: true
          })
        }
      });
    }else{
      wx.showToast({
        title: '手机号码错误',
        icon: 'succes',
        image: '../../images/err.png',
        duration: 2000,
        mask: true
      })
    }
  },

  // 验证码
  Code: function(e){
    this.setData({
      Code: e.detail.value,
    });
  },

  // 点击添加按钮
  AddBank: function () {
    var that = this;
    var Code = that.data.Code
    if (Code != null && Code != ''){
      $.Ajax("api/x_add_user_card", "POST", {
        bank_id: that.data.BankType,
        name: that.data.Name,
        subbranch: that.data.Name_,
        number: that.data.Number,
        mobile: that.data.Phone,
        code: Code
      }, function (res) {
        console.log(res)
        if (res.s) {
          wx.showToast({
            title: '成功',
            icon: 'succes',
            duration: 3000,
            mask: true
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 3000)
        }else{
          wx.showToast({
            title: res.m,
            icon: 'succes',
            image: '../../images/err.png',
            duration: 3000,
            mask: true
          })
        }
      });
    }else{
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }
  },

  // 收不到验证码
  NoCode: function(){
    this.setData({
      CodeBg: false
    })
  },

  // 关闭遮罩层
  NoCodeBg: function(){
    this.setData({
      CodeBg: true
    })
  }

})